package org.gushi.datastruct.a03;

/**
 * 完成双向循环链表的实现
 * @param <E>
 */
public class CircleDoubleLinedList<E> extends AbstractList<E> {

    private Node<E> first;
    private Node<E> end;

    private static class Node<E> {
        private E element;

        private Node<E> prov;
        private Node<E> next;

        public Node(E element, Node<E> prov, Node<E> next) {
            this.element = element;
            this.prov = prov;
            this.next = next;
        }
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        end = null;
    }

    @Override
    public E get(int index) {
        Node<E> node = getNode(index);
        return node.element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = getNode(index);
        Node<E> old = node;
        node.element = element;
        return old.element;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == size) {
            Node<E> oldEnd = end;
            end = new Node<>(element, end, null);
            if (oldEnd == null) {//最后的位置并且是最开始的位置-添加元素
                first = end;
            }else{//最后的位置但是不是最开始的位置-添加元素
                oldEnd.next = end;
            }

        } else {
            Node<E> next = getNode(index);
            Node<E> prov = next.prov;
            Node<E> node = new Node<>(element, prov, next);//新元素

            if (prov == null) { // index = 0  最开始添加元素，但是后面有元素
                first = node;
            } else { //中间的情况
                prov.next = node;
            }
        }

        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = getNode(index);
        Node<E> prev = node.prov;
        Node<E> next = node.next;

        if(prev==null){//代表是最开始的元素
            first = next;
        }else{
            prev.next = next;
        }

        if(next==null){//代表是最后的元素
            end = prev;
        }else{
            next.prov = prev;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        if(element==null){
            CircleDoubleLinedList.Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        }else {
            CircleDoubleLinedList.Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    private Node<E> getNode(int index) {
        rangeCheck(index);
        Node<E> res;

        if (index < (size >> 1)) {
            Node<E> node = first;
            System.out.println("从头开始遍历");
            //从头开始遍历
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            res = node;
        } else {
            Node<E> nodeEnd = end;
            System.out.println("从尾部开始遍历");
            //从尾部开始
            for (int i = size; i > size-index+1; i--) {
                nodeEnd = nodeEnd.prov;
            }
            res = nodeEnd;
        }
        return res;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        CircleDoubleLinedList.Node<E> node = first;
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(node.element);

            node = node.next;
        }
        string.append("]");
        return string.toString();
    }
}
